package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;

/**
 * Write a description of class OrderUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class OrderUI
{
    // instance variables - replace the example below with your own
    private PartOrderUI partUI;
    private OrderController oCtr;

    /**
     * Constructor for objects of class OrderUI
     */
    public OrderUI()
    {
        // initialise instance variables
        oCtr = OrderController.getInstance();
    }

    public void start()
    {
        Menu();        
    }

    public void Menu()
    {
        boolean exit = false;

        while(!exit)  //if exit = true, the main menu will shut down 
        {
            int choice = writeMainMenu();
            switch(choice)
            {
                case 1:
                create();
                break;
                case 2:
                read();
                break;
                case 3:
                update();
                break;                
                case 4:
                delete();
                break;
                case 5:
                printID();
                break;
                case 6:
                printAll();
                break;
                case 7:
                partUI();
                break;
                default:                
                exit = true;
                break; 
            }
        }
    }

    public int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ****Order Menu***");
        System.out.println("  (1) Create");
        System.out.println("  (2) Read");
        System.out.println("  (3) Update");
        System.out.println("  (4) Delete");
        System.out.println("  (5) Print ID");
        System.out.println("  (6) Print ALL");
        System.out.println("  (7) Add partOrder to Order");
        System.out.println("  (8) Return");
        System.out.println("\n make your choice: ");

        int choice = keyboard.nextInt();

        return choice;

    }

    public void partUI()
    {
        partUI = new PartOrderUI();
        partUI.start();
    }

    private long inputID()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Order id: " );
        long out = keyboard.nextLong();
        return out;
    }  

    private long inputIDSalesman()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);        
        System.out.println("Type Salesman id:  " );
        long out = keyboard.nextLong();
        return out;
    }

    private long inputIDCostumer()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);        
        System.out.println("Type Costumer id: //If typed number doesn't contain any Costumer, it will be 'null' " );
        long out = keyboard.nextLong();
        return out;
    }

    private boolean inputDone()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type if active or not (y/n): " );
        String answer = keyboard.nextLine();
        if(answer == "y")
        {
            return true;
        }
        else 
        {
            return false;
        }
    } 

    private boolean inputYesOrNo()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Do you wanna update (y/n): " );
        String answer = keyboard.nextLine();
        if(answer == "y")
        {
            return true;
        }
        else 
        {
            return false;
        }
    } 

    public void create()
    {
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("      To create an order you have to type an existing salesmanID");
        keyboard.nextLine();

        long salesmanID = inputIDSalesman();

        if(salesmanID > 0)
        {
            oCtr.createOrder(inputDone(), inputIDCostumer(), salesmanID);
            long id = oCtr.getID();
            System.out.println("     new Order has been created - se info under");
            oCtr.printOneOrder(id);
            System.out.println(" Order code nr.: "+oCtr.readOrder(id));
        }
        else
        {
            System.out.println("    Order is not created - try another salesmanID");
            System.out.println("    Required an salesman ID");
        }        
        
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void read()
    {        
        Scanner keyboard = new Scanner(System.in);

        long id = inputID();
        if(oCtr.readOrder(id) != null)
        {
            System.out.println("        Order was found - se info under");
            oCtr.printOneOrder(id);
            System.out.println(" Oder code nr.: "+oCtr.readOrder(id));
        }
        else
        {
            System.out.println("  Order was not found - try another ID");
        }
        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void update()
    {
        Scanner keyboard = new Scanner(System.in);

        long idO = inputID();

        if(oCtr.readOrder(idO) != null)
        {
            oCtr.printOneOrder(idO);
            System.out.println();            
            //             boolean yesORno = inputYesOrNo();

            oCtr.updateOrder(idO, inputDone(), inputIDCostumer(), inputIDSalesman());
            System.out.println("       Order has been updated - se info under");
            oCtr.printOneOrder(idO);
            System.out.println(" Oder code nr.: "+oCtr.readOrder(idO));
            //             if(yesORno == true)
            //             {                
            //                 oCtr.updateOrder(idO, inputDone(), inputIDCostumer(), inputIDSalesman());
            //                 System.out.println("       Order has been updated - se info under");
            //                 oCtr.printOneOrder(idO);
            //                 System.out.println(" Oder code nr.: "+oCtr.readOrder(idO));
            //             }
            //             else
            //             {
            //                 System.out.println("    Nothing has been changed");
            //             }
        }
        else
        {
            System.out.println("  Order was not found - try another ID");
        }        

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void delete()
    {
        Scanner keyboard = new Scanner(System.in);

        long idO = inputID();

        if(oCtr.readOrder(idO) != null)
        {
            System.out.println("       Order is deleted - se info under");
            oCtr.printOneOrder(idO);
            oCtr.deleteOrder(idO);
        }
        else
        {
            System.out.println("  Order was not found - try another ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printID()
    {
        Scanner keyboard = new Scanner(System.in);

        long idO = inputID();

        if(oCtr.readOrder(idO) != null)
        {
            oCtr.printOneOrder(idO);
        }
        else
        {
            System.out.println("  Order was not found - try another ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printAll()
    {
        Scanner keyboard = new Scanner(System.in);

        if(oCtr.getID() != 0)
        {
            oCtr.printAllOrder();
        }
        else
        {
            System.out.println("  No Order was not found - create a Order");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }
}
